package com.barryredmond.javamelibchecker;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * 
 * @author Barry Redmond
 *
 */
public class LibCheckerMIDlet extends MIDlet {

    private static MIDlet midlet;
    
    Displayable currentDisplayable = null;
    
    public LibCheckerMIDlet(){
        midlet = this;
        JsrCheckerForm mainForm = new JsrCheckerForm();
        Display.getDisplay(this).setCurrent(mainForm);
    }
    
    public static final MIDlet getMIDlet(){
        return midlet;
    }
    
    /**
     * <p>Signals the MIDlet to terminate and enter the Destroyed state. 
     * In the destroyed state the MIDlet must release all resources and save any persistent state. 
     * This method may be called from the Paused or Active states.</p>
     * 
     * <p>MIDlets should perform any operations required before being terminated, 
     * such as releasing resources or saving preferences or state.</p>
     * 
     * <p>NOTE: The MIDlet can request that it not enter the Destroyed  state by throwing an MIDletStateChangeException. 
     * This is only a valid response if the unconditional  flag is set to false. 
     * If it is true  the MIDlet is assumed to be in the Destroyed state regardless of how this method terminates. 
     * If it is not an unconditional request, the MIDlet can signify that it wishes to stay in its current 
     * state by throwing the MIDletStateChangeException. This request may be honored and the destroy()  
     * method called again at a later time.</p>
     * 
     * <p>If a Runtime exception occurs during destroyApp then they are 
     * ignored and the MIDlet is put into the Destroyed state.</p>
     * 
     * @param unconditional If true when this method is called, the MIDlet must cleanup and release all resources. 
     * If false the MIDlet may throw MIDletStateChangeException to indicate it does not want to be destroyed at this time.
     * 
     * @throws MIDletStateChangeException is thrown if the MIDlet wishes to continue to execute (Not enter the Destroyed state). 
     * This exception is ignored if unconditional is equal to true.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException{
        // Nothing to do on 'destroy'
    }

    /**
     * <p>Signals the MIDlet to stop and enter the Paused state.
     * In the Paused state the MIDlet must release shared resources and become quiescent. 
     * This method will only be called called when the MIDlet is in the Active state.</p>
     * 
     * <p>If a Runtime exception occurs during pauseApp the MIDlet will be destroyed immediately. 
     * Its destroyApp will be called allowing the MIDlet to cleanup.</p>
     */
    protected void pauseApp() {
        // Save the state:
        this.currentDisplayable = Display.getDisplay(this).getCurrent();
    }

    /**
     * <p>Signals the MIDlet that it has entered the Active state. 
     * In the Active state the MIDlet may hold resources. 
     * The method will only be called when the MIDlet is in the Paused state.</p>
     * 
     * <p>Two kinds of failures can prevent the service from starting, transient and non-transient. 
     * For transient failures the MIDletStateChangeException exception should be thrown. 
     * For non-transient failures the notifyDestroyed method should be called.</p>
     * 
     * <p>If a Runtime exception occurs during startApp the MIDlet will be destroyed immediately. 
     * Its destroyApp will be called allowing the MIDlet to cleanup.</p>
     * 
     * @throws MIDletStateChangeException is thrown if the MIDlet cannot start now but might be able to start at a later time.
     */
    protected void startApp() throws MIDletStateChangeException {
        // Revert the state:
        if( currentDisplayable!=null ){
            Display.getDisplay(this).setCurrent(currentDisplayable);
        }
    }

}
